<template>
  <div class="register-container container">
    <h2>注册</h2>
    <van-form @submit="onSubmit" validate-trigger="onChange">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="phone"
        name="phone"
        label="手机号"
        placeholder="手机号，可选"
      />
      <FormEmail v-model="emailData" />
      <van-field
        v-model="password"
        type="password"
        name="pswd"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/,
            message: '密码需要由字母+数字组成且8-20位组成'
          }
        ]"
      />
      <div class="submit">
        <van-button round block type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
    <div class="bottom-link">
      <nuxt-link to="/login">我已有账号,去登录</nuxt-link>
    </div>
  </div>
</template>
<script>
export default {
  layout: "notabbar",
  data() {
    return {
      username: "",
      email: "",
      phone: "",
      password: "",
      emailData: {}
    };
  },
  methods: {
    onSubmit(val) {
      this.$apiRegister(val).then(res => {
        const { status, msg } = res;
        if (status === 0) {
          this.$toast({ message: msg, type: "success" });
          setTimeout(() => {
            this.$router.replace("/login");
          }, 1500);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped src="../login/index.scss"></style>
